/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogFornecedor.java
 *
 * Created on 12/04/2011, 12:12:02
 */

package camada.apresentacao.dialog.fornecedor;

import camada.apresentacao.dialog.cliente.DialogCadCliente;
import camada.bancodedados.daoimplemente.fornecedor.FornecedorDAOImpl;
import eloma.classe.Fornecedor;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import tabelamodelcolumnmodel.fornecedor.FornecedorColumnModel;
import tabelamodelcolumnmodel.fornecedor.FornecedorTableModel;
import utilitarios.Exceptions;

/**
 *
 * @author icaro.klinger
 */
public class DialogFornecedor extends javax.swing.JDialog {

    FornecedorDAOImpl daoFornecedor = new FornecedorDAOImpl();
    List<Fornecedor> listaFornecedor = new ArrayList<Fornecedor>();
    Exceptions exceptions = new Exceptions();
    
    boolean pesquisa = false;
    private int indexTabela;

    /** Creates new form DialogFornecedor */
    public DialogFornecedor(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        this.setTitle("Forncedores");
        configurarTabela(null);
    }

     public void configurarTabela(List<Fornecedor> lista) {
        try {

            if (lista == null) {
                listaFornecedor = daoFornecedor.selecionarTodosFornecedor();
            } else {
                listaFornecedor = lista;
            }

            setListaFornecedor(listaFornecedor);

            tabelaFornecedor.setAutoCreateColumnsFromModel(false);
            java.awt.FontMetrics fm = tabelaFornecedor.getFontMetrics(tabelaFornecedor.getFont());

            tabelaFornecedor.setColumnModel(new FornecedorColumnModel(fm));
            tabelaFornecedor.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        } catch (Exception e) {
             exceptions.Exceptions(e.getMessage().toString(), "DialogCliente-configurarTabela");
        }
    }

      public void setListaFornecedor(List<Fornecedor> listFornecedor) {
        tabelaFornecedor.setModel(new FornecedorTableModel(listFornecedor));
    }
     
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btRemover = new javax.swing.JButton();
        txPesquisa = new javax.swing.JTextField();
        btVoltar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        btAdicionar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        btEditar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaFornecedor = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        btRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Remove.png"))); // NOI18N
        btRemover.setText("Remover");
        btRemover.setEnabled(false);
        btRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoverActionPerformed(evt);
            }
        });

        txPesquisa.setForeground(new java.awt.Color(153, 153, 153));
        txPesquisa.setText("Digite um nome para ser pesquisado");
        txPesquisa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txPesquisaMouseClicked(evt);
            }
        });
        txPesquisa.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txPesquisaFocusLost(evt);
            }
        });

        btVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Voltar.png"))); // NOI18N
        btVoltar.setText("Voltar");
        btVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btVoltarActionPerformed(evt);
            }
        });

        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Cancel.png"))); // NOI18N
        btCancelar.setText("Cancelar");
        btCancelar.setEnabled(false);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        btAdicionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Add.png"))); // NOI18N
        btAdicionar.setText("Adicionar");
        btAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAdicionarActionPerformed(evt);
            }
        });

        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Editar.png"))); // NOI18N
        btEditar.setText("Editar");
        btEditar.setEnabled(false);
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/outros/Pesquisa.png"))); // NOI18N
        jButton1.setText("Pesquisar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        tabelaFornecedor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabelaFornecedor.setToolTipText("Tabela de Clientes Cadastrados");
        tabelaFornecedor.setSelectionBackground(new java.awt.Color(255, 255, 0));
        tabelaFornecedor.setSelectionForeground(new java.awt.Color(0, 0, 0));
        tabelaFornecedor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaFornecedorMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelaFornecedor);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btEditar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btRemover)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btVoltar)
                        .addContainerGap(157, Short.MAX_VALUE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 630, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btAdicionar)
                    .addComponent(btEditar)
                    .addComponent(btRemover)
                    .addComponent(btCancelar)
                    .addComponent(btVoltar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoverActionPerformed
        try{
            
            FornecedorTableModel model = (FornecedorTableModel)tabelaFornecedor.getModel();
            Fornecedor fornecedor = model.getValoresFirnecedor(indexTabela);
            
            daoFornecedor.removerFornecedor(fornecedor.getIdFornecedor());
            
            configurarTabela(null);
            
            btAdicionar.setEnabled(true);
            btCancelar.setEnabled(false);
            btEditar.setEnabled(false);
            btRemover.setEnabled(false);
            btVoltar.setEnabled(true);
            
        }catch(Exception e){
            
        }
}//GEN-LAST:event_btRemoverActionPerformed

    private void txPesquisaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txPesquisaMouseClicked
        if (pesquisa == false) {
            txPesquisa.setText("");
            txPesquisa.setForeground(Color.white);
            pesquisa = true;
        }
}//GEN-LAST:event_txPesquisaMouseClicked

    private void txPesquisaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txPesquisaFocusLost
        if(txPesquisa.getText() == null ? "" == null : txPesquisa.getText().equals("")){
            txPesquisa.setForeground(new Color(153,153,153));
            txPesquisa.setText("Digite um nome para ser pesquisado");
            pesquisa = false;
        }
}//GEN-LAST:event_txPesquisaFocusLost

    private void btVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btVoltarActionPerformed
        this.hide();
}//GEN-LAST:event_btVoltarActionPerformed

    private void btAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAdicionarActionPerformed
        DialogCadFornecedor cadFornecedor = new DialogCadFornecedor(null, true);
        cadFornecedor.show();
        configurarTabela(null);
}//GEN-LAST:event_btAdicionarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {


        } catch (Exception e) {
        }
}//GEN-LAST:event_jButton1ActionPerformed

    private void tabelaFornecedorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaFornecedorMouseClicked
        try {

            if (evt.getClickCount() == 2) {
            
                JOptionPane.showMessageDialog(null, "dois Clicks...");
                
            } else {
            
                indexTabela = tabelaFornecedor.getSelectedRow();

                if (indexTabela > -1) {

                    btAdicionar.setEnabled(false);
                    btVoltar.setEnabled(false);
                    btEditar.setEnabled(true);
                    btRemover.setEnabled(true);
                    btCancelar.setEnabled(true);
                }
            }
        } catch (Exception e) {

        }
}//GEN-LAST:event_tabelaFornecedorMouseClicked

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        try{
            
            FornecedorTableModel model = (FornecedorTableModel)tabelaFornecedor.getModel();
            Fornecedor fornecedor = model.getValoresFirnecedor(indexTabela);
            
            DialogCadFornecedor cadFornecedor = new DialogCadFornecedor(null, true);
            cadFornecedor.carregarEdicao(fornecedor);
            cadFornecedor.show();
            configurarTabela(null);
            
        }catch(Exception e){
            exceptions.Exceptions(e.getMessage(), "DialogCliente-btEditarActionPerformed");
        }
    }//GEN-LAST:event_btEditarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
           btAdicionar.setEnabled(true);
            btCancelar.setEnabled(false);
            btEditar.setEnabled(false);
            btRemover.setEnabled(false);
            btVoltar.setEnabled(true);
    }//GEN-LAST:event_btCancelarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogFornecedor dialog = new DialogFornecedor(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdicionar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btEditar;
    private javax.swing.JButton btRemover;
    private javax.swing.JButton btVoltar;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable tabelaFornecedor;
    private javax.swing.JTextField txPesquisa;
    // End of variables declaration//GEN-END:variables

}
